
import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { ElNotification } from 'element-plus'
import { useRouter } from 'vue-router'


export const useInfoStore = defineStore('userinfo', () => {
  const router = useRouter()
  //  nihaoya
  const token = ref()
  const userLogin =  (data:any) => {
      axios({
        method: 'post',
        url: '/login',
        data
      }).then((res) => {
        token.value = res.data.token
      ElNotification({
       title: '欢迎~',
       message: '登录成功',
       duration: 2000,
       type: 'success',
       onClose: () => {
         router.replace('/')
       }
      })
      })
  }

  //退出登录
  const userLogout = async () => {
  const res = await axios.post('/getPermissions', {token:token.value})
  let permissionsList = res.data.permissions
    permissionsList.forEach((item:any) => {
    router.removeRoute(item.name)
  })
    token.value = null
    ElMessage({
      message: '退出成功',
      type: 'success',
      duration: 2000,
      onClose: () => {
        router.replace('/login')
      }
    })
  }

  // 计算商品价格
  

  return {
    token,
    userLogin,
    userLogout
   }
}, {
  persist: true
})
